package lc.q300_350;

import java.util.Arrays;

public class Q313 {
    public int nthSuperUglyNumber(int n, int[] primes) {
        int[] point = new int[primes.length];
        Arrays.fill(point, 1);
        // 从1开始
        int[] dp = new int[n + 1];
        dp[0] = 1;
        dp[1] = 1;
        for (int i = 2; i <= n; i++) {
            int num = Integer.MAX_VALUE;
            for (int i1 = 0; i1 < point.length; i1++) {
                num = Integer.min(primes[i1] * dp[point[i1]], num);
            }
            for (int i1 = 0; i1 < point.length; i1++) {
                if (primes[i1] * dp[point[i1]] == num) {
                    point[i1] ++;
                }
            }
            dp[i] = num;
        }
        return dp[n];
    }

    public static void main(String[] args) {
        System.out.println(new Q313().nthSuperUglyNumber(12, new int[]{2, 7, 13, 19}));
    }
}
